package net.lab1024.sa.admin.module.business.erp.mmo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.MmordaDao;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MmordaEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.MmordaAddForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.MmordaQueryForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.MmordaUpdateForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.MmordaVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 生产订单与机器对应表 Service
 *
 * @Author AKITA
 * @Date 2025-04-19 12:46:38
 * @Copyright AKITA
 */

@Service
public class MmordaService {

    @Resource
    private MmordaDao mmordaDao;


    public PageResult<MmordaVO> queryPage(MmordaQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<MmordaVO> list = mmordaDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    @Transactional
    public ResponseDTO<String> add(MmordaAddForm addForm) {
        MmordaEntity mmordaEntity = SmartBeanUtil.copy(addForm, MmordaEntity.class);
        int i = mmordaDao.getMaxLineNum(addForm.getId()) == null ? 1 : mmordaDao.getMaxLineNum(addForm.getId()) + 1;
        mmordaEntity.setLineNum(i);
        mmordaDao.insert(mmordaEntity);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> update(MmordaUpdateForm updateForm) {
        MmordaEntity mmordaEntity = SmartBeanUtil.copy(updateForm, MmordaEntity.class);
        mmordaDao.update(mmordaEntity, new LambdaQueryWrapper<MmordaEntity>().eq(MmordaEntity::getId, updateForm.getId()).eq(MmordaEntity::getLineNum, updateForm.getLineNum()));
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        mmordaDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }


    public ResponseDTO<String> delete(Long docEntry) {
        mmordaDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> deleteLine(MmordaAddForm addForm) {
        mmordaDao.delete(new LambdaQueryWrapper<MmordaEntity>().eq(MmordaEntity::getId, addForm.getId()).eq(MmordaEntity::getLineNum, addForm.getLineNum()));
        return ResponseDTO.ok();
    }
}
